package org.df.ulab.bot.util.communication;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.df.ulab.bot.api.communication.CommunicationService;
import org.df.ulab.bot.api.message.Message;

/**
 * @author Daniel Filgueiras
 * @since 19/06/2011
 */
public abstract class BaseCommunicationService implements CommunicationService {

	protected boolean keepAlive = true;
	private final int id = TOKEN.getAndIncrement();
	private BlockingQueue<Message> incomeMessages = new LinkedBlockingQueue<Message>();
	private BlockingQueue<Message> outcomeMessages = new LinkedBlockingQueue<Message>();
	
	protected void addIncome(Message message) {
		incomeMessages.add(message);
	}
	protected Message getNextMessage() {
		return outcomeMessages.poll();
	}
	
	@Override
	public int getUniqueId() {
		return id;
	}

	@Override
	public boolean stopService() {
		return keepAlive = false;
	}

	@Override
	public BlockingQueue<Message> getIncome() {
		return incomeMessages;
	}

	@Override
	public boolean post(Message mesage) {
		return outcomeMessages.add(mesage);
	}
	
}
